﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using MouseEventArgs = System.Windows.Forms.MouseEventArgs;

namespace CountdownTool
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private NotifyIcon _notifyIcon;
        private readonly TimeShow _timeShowWindow;
        private bool _hasAdjust = true;

        public MainWindow()
        {
            InitializeComponent();
            InitNotifyIcon();
            this.Closing += MainWindow_Closing;

            _timeShowWindow = new TimeShow(new SettingViewModel());
            this.DataContext = _timeShowWindow.Setting;
        }

        private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            HideMainWindow();
            e.Cancel = true;
        }

        private void InitNotifyIcon()
        {
            _notifyIcon = new NotifyIcon
            {
                BalloonTipText = "CountdownTool",
                Text = "CountdownTool"
            };

            _notifyIcon.ShowBalloonTip(2000);
            _notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
            _notifyIcon.Visible = true;
            _notifyIcon.MouseDoubleClick += ShowMainWindow;
            _notifyIcon.MouseClick += ShowMainWindow;
        }

        private void ShowMainWindow(object sender, MouseEventArgs e)
        {
            this.Show();
            this.ShowInTaskbar = true;
        }

        private void HideMainWindow()
        {
            this.Hide();
            this.ShowInTaskbar = false;
        }



        private void StartCountdownBtn_OnClick(object sender, RoutedEventArgs e)
        {
            if (_hasAdjust)
            {
                _timeShowWindow.Setting.Reset();
            }
            _timeShowWindow.StartCountdown();
            _hasAdjust = false;
        }

        private void ResetCountdownBtn_OnClick(object sender, RoutedEventArgs e)
        {
            _timeShowWindow.ResetCountdown();
        }

        private void StopCountdownBtn_OnClick(object sender, RoutedEventArgs e)
        {
            _timeShowWindow.StopCountdown();
        }

        private void MinuteAdd_OnClick(object sender, RoutedEventArgs e)
        {
            _timeShowWindow.Setting.MinuteValue++;
            _hasAdjust = true;
        }

        private void MinuteSub_OnClick(object sender, RoutedEventArgs e)
        {
            _timeShowWindow.Setting.MinuteValue--;
            _hasAdjust = true;
        }

        private void SecondAdd_OnClick(object sender, RoutedEventArgs e)
        {
            _timeShowWindow.Setting.SecondValue++;
            _hasAdjust = true;
        }

        private void SecondeSub_OnClick(object sender, RoutedEventArgs e)
        {
            _timeShowWindow.Setting.SecondValue--;
            _hasAdjust = true;
        }

        private void IntervalAdd_OnClick(object sender, RoutedEventArgs e)
        {
            _timeShowWindow.Setting.IntervalValue++;
        }

        private void IntervalSub_OnClick(object sender, RoutedEventArgs e)
        {
            _timeShowWindow.Setting.IntervalValue--;
        }

        private void HoldOnBtn_OnClick(object sender, RoutedEventArgs e)
        {
            _timeShowWindow.Setting.IsHoldOn = !_timeShowWindow.Setting.IsHoldOn;
            if (_timeShowWindow.IsRunning)
            {
                _timeShowWindow.ShowInShortTime();
            }
        }

        private void ExitWindowBtn_OnClick(object sender, RoutedEventArgs e)
        {
            System.Windows.Application.Current.Shutdown(0);
        }

        private void HideCountdownBtn_OnClick(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void ShowOnLeftBtn_OnClick(object sender, RoutedEventArgs e)
        {
            _timeShowWindow.Setting.TimeWindowLocation = TimeWindowLocation.LeftUp;
            _timeShowWindow.ShowOnLeftOrRight();
        }

        private void ShowInRightBtn_OnClick(object sender, RoutedEventArgs e)
        {
            _timeShowWindow.Setting.TimeWindowLocation = TimeWindowLocation.RightUp;
            _timeShowWindow.ShowOnLeftOrRight();
        }
    }
}
